package com.sld.concrete.vehicle.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.sld.concrete.vehicle.domain.InfoVehicle;
import com.sld.concrete.vehicle.service.IInfoVehicleService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 车辆管理Controller
 * 
 * @author topviewers
 * @date 2022-03-13
 */
@Controller
@RequestMapping("/device/vehicle")
public class InfoVehicleController extends BaseController
{
    private String prefix = "device/vehicle";

    @Autowired
    private IInfoVehicleService infoVehicleService;

    @RequiresPermissions("device:vehicle:view")
    @GetMapping()
    public String vehicle()
    {
        return prefix + "/vehicle";
    }

    /**
     * 查询车辆管理列表
     */
    @RequiresPermissions("device:vehicle:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(InfoVehicle infoVehicle)
    {
        startPage();
        List<InfoVehicle> list = infoVehicleService.selectInfoVehicleList(infoVehicle);
        return getDataTable(list);
    }

    /**
     * 导出车辆管理列表
     */
    @RequiresPermissions("device:vehicle:export")
    @Log(title = "车辆管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(InfoVehicle infoVehicle)
    {
        List<InfoVehicle> list = infoVehicleService.selectInfoVehicleList(infoVehicle);
        ExcelUtil<InfoVehicle> util = new ExcelUtil<InfoVehicle>(InfoVehicle.class);
        return util.exportExcel(list, "车辆管理数据");
    }

    /**
     * 新增车辆管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存车辆管理
     */
    @RequiresPermissions("device:vehicle:add")
    @Log(title = "车辆管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(InfoVehicle infoVehicle){
        try {
            return toAjax(infoVehicleService.insertInfoVehicle(infoVehicle));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 修改车辆管理
     */
    @RequiresPermissions("device:vehicle:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        InfoVehicle infoVehicle = infoVehicleService.selectInfoVehicleById(id);
        mmap.put("infoVehicle", infoVehicle);
        return prefix + "/edit";
    }

    /**
     * 修改保存车辆管理
     */
    @RequiresPermissions("device:vehicle:edit")
    @Log(title = "车辆管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(InfoVehicle infoVehicle)
    {
        return toAjax(infoVehicleService.updateInfoVehicle(infoVehicle));
    }

    /**
     * 删除车辆管理
     */
    @RequiresPermissions("device:vehicle:remove")
    @Log(title = "车辆管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(infoVehicleService.deleteInfoVehicleByIds(ids));
    }
}
